<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Api\Service\AuthService;
use App\Http\Controllers\Controller;
use Exception;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use App\Models\Sys\Role;

class RoleController extends Controller
{

    public function list(Request $request)
    {
        try {
            $param = $request->all();
            $Role = new Role();
            $data = $Role->getAllRole($param);
            return success($data);
        } catch (Exception $e) {
            return error(null, $e->getMessage());
        }
    }
    public function add(Request $request)
    {
        try {
            $data = $request->all();
            $validator = Validator::make(
                $data,
                [
                    'code' => 'required|unique:sys_role,code',
                    'name' => 'required'
                ],
                [
                    'code.required' => 'code不能为空',
                    'code.unique' => 'code必须唯一',
                    'name.required' => '角色名称不能为空',
                ]
            );
            if ($validator->fails()) {
                throw new Exception($validator->errors()->first());
            }
            $Role = new Role();
            $Role->code = $data['code'];
            $Role->name = $data['name'];
            $res = $Role->save();
            if (!$res) {
                throw new Exception('添加失败');
            }
            return success();
        } catch (Exception $e) {
            return error(null, $e->getMessage());
        }
    }

    public function delete(Request $request)
    {
        try {
            $data = $request->all();
            $validator = Validator::make(
                $data,
                [
                    'id' => 'required|exists:sys_role,id',
                ],
                [
                    'id.required' => 'id不能为空',
                    'id.exists' => '删除的角色不存在',
                ]
            );
            if ($validator->fails()) {
                throw new Exception($validator->errors()->first());
            }
            $Role = new Role();
            $data = $Role->where('id', '=', $request->id)->delete();
            return success($data);
        } catch (Exception $e) {
            return error(null, $e->getMessage());
        }
    }


    public function edit(Request $request)
    {
        try {
            $data = $request->all();
            $validator = Validator::make(
                $data,
                [
                    'id' => 'required'
                ],
                [
                    'id.required' => 'id不能为空',
                ]
            );
            if ($validator->fails()) {
                throw new Exception($validator->errors()->first());
            }
            $Role = new Role();
            $res = $Role->find($data['id']);
            if (!$res) {
                throw new Exception('查询失败');
            }
            return success($res);
        } catch (Exception $e) {
            return error(null, $e->getMessage());
        }
    }


    //获取某一角色所有权限
    public function getMenuByRole(Request $request)
    {
        try {
            $data = $request->all();
            $validator = Validator::make(
                $data,
                [
                    'id' => 'required',
                ],
                [
                    'id.required' => 'id不能为空',
                ]
            );
            if ($validator->fails()) {
                throw new Exception($validator->errors()->first());
            }
            $authService = new AuthService();
            $result = $authService->getMenuByRoleId($data['id']);
            //获取角色权限
            $son = getTree($result);
            $sonId = [];
            foreach ($son as $v1) {
                if (isset($v1['children']) && count($v1['children']) > 0) {
                    foreach ($v1['children'] as $v2) {
                        $sonId[] = $v2['id'];
                    }
                } else {
                    $sonId[] = $v1['id'];
                }
            }
            return success($sonId);
        } catch (Exception $e) {
            return error(null, $e->getMessage());
        }
    }


    //保存角色权限
    public function saveRoleMenu(Request $request)
    {
        try {
            $data = $request->all();
            $validator = Validator::make(
                $data,
                [
                    'id' => 'required',
                    'menu' => 'required|array'
                ],
                [
                    'id.required' => 'id不能为空',
                    'menu.required' => 'menu不能为空',
                    'menu.array' => 'menu必须是数组',
                ]
            );
            if ($validator->fails()) {
                throw new Exception($validator->errors()->first());
            }
            $res = (new Role())->saveMenu($data['id'], $data['menu']);
            if (!$res) {
                throw new Exception('更新失败');
            }
            return success();
        } catch (Exception $e) {
            return error(null, $e->getMessage());
        }
    }
}
